{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ```Experiment``` class for live in-line plotting with jupyter\n",
    "This example uses the ```Experiment``` class to create a measurement from a ```procedure``` object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%writefile procedures.py\n",
    "import random\n",
    "from time import sleep\n",
    "\n",
    "import logging\n",
    "log = logging.getLogger('')\n",
    "log.addHandler(logging.NullHandler())\n",
    "\n",
    "from pymeasure.experiment import Procedure, IntegerParameter, Parameter, FloatParameter\n",
    "\n",
    "class TestProcedure(Procedure):\n",
    "\n",
    "    iterations = IntegerParameter('Loop Iterations', default=100)\n",
    "    delay = FloatParameter('Delay Time', units='s', default=0.2)\n",
    "    seed = Parameter('Random Seed', default='12345')\n",
    "\n",
    "    DATA_COLUMNS = ['Iteration', 'Random Number']\n",
    "\n",
    "    def startup(self):\n",
    "        log.info(\"Setting up random number generator\")\n",
    "        random.seed(self.seed)\n",
    "\n",
    "    def execute(self):\n",
    "        log.info(\"Starting to generate numbers\")\n",
    "        for i in range(self.iterations):\n",
    "            data = {\n",
    "                'Iteration': i,\n",
    "                'Random Number': random.random()\n",
    "            }\n",
    "            log.debug(\"Produced numbers: %s\" % data)\n",
    "            self.emit('results', data)\n",
    "            self.emit('progress', 100.*i/self.iterations)\n",
    "            sleep(self.delay)\n",
    "            if self.should_stop():\n",
    "                log.warning(\"Catch stop command in procedure\")\n",
    "                break\n",
    "\n",
    "    def shutdown(self):\n",
    "        log.info(\"Finished\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pymeasure.experiment import Experiment\n",
    "from procedures import TestProcedure\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "experiment = Experiment('test', TestProcedure(iterations=100, delay=.1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "experiment.start()\n",
    "experiment.plot_live('Iteration', 'Random Number')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
